#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include "protocol.h"
#include "database.h"

#define DATABASE "warehouse.db"

#define THRESHOLD "insert into threshold_value values(50,5,50,10,500,10)"

#define MSG msg_t




void data_init(sqlite3 *db1,sqlite3 *db2,sqlite3 *db3,char * errmsg)
{
//	char buf[200] = {};
//	env_msg env; 				//多线程传参用的连接结构体
//	goods_msg goods;
//	threshold_t value;
	&table_t.warehouse = db1;
	&table_t.goods = db2;
	&table_t.humidity = db3;


	if(sqlite3_open(DATABASE,&db) !=SQLITE_OK){
		errmsg = sqlite3_errmsg(db);
		fprintf(stderr,"%s\n",errmsg);
		exit(1);
	}
	printf("打开成功！");


	//创建仓库环境表格
	char *create_table = "create table if not exists warehouse(led,beep,fan,seg,x,y,z,temperature,humidity,battery,info_time)";
	if(sqlite3_exec(table_t.warehouse,create_table,NULL,NULL,&errmsg) != SQLITE_OK){
		fprintf(stderr,"%s\n",errmsg);
		exit(1);
	}
	else printf("open/create warehouseform success!");

	//创建货物表格
	char *create_table1 = "create table if not exists goods_info(warehouse_no,goods_no,info_time,empty_status)";
	if(sqlite3_exec1(table_t.goods,create_table,NULL,NULL,&errmsg) != SQLITE_OK){
		fprintf(stderr,"%s\n",errmsg);
		exit(1);
	}
	else printf("open/create goodsform success!");
	
	//创建阈值表格
	char *create_table2 = "create table if not exists threshold_value(temperatureMAX,temperatureMIN.humidityMAX,humidityMIN,illuminationMAX,illuminationMIN)";
	if(sqlite3_exec(table_t.humidity,create_table,NULL,NULL,&errmsg) != SQLITE_OK){
		fprintf(stderr,"%s\n",errmsg);
		exit(1);
	}
	else printf("open/create threshold_valueform success!");


}

//表的选择
void table_choice()
{
	char buf[200] = {};
	database_option data;

	if(data.table_select_mask == 0){
		buf= warehouse_operation_mask;
	}
}




// 命令封装
void command_rule(int socket_fd,goods_msg *goods,threshold_t *th)
{
	int n;
//	MSG msg;
//	goods_msg goods;
	threshold_t th;
	//表选择
//	void table_choice();

	scanf("%d",&n);

		switch(n)
		{
			case 1:
				//货物添加
				goods_add(socket_fd,goods);
				//阈值添加
				humidity_add(socket_fd,th);
				break;
			case 2:
				//货物修改
				goods_change(socket_fd,goods);
				//阈值修改
				humidity_change(socket_fd,th);
				break;
			case 3:
				//查询
				admin_query(socket_fd,goods);
				break;
			case 4:
				printf("退出！");
				break;
			default:
				printf("输入错误,重新选择！");
		}
	}
}





void goods_add(int socket_fd,goods_msg *goods)
{
	char buf[200] = {0};
	char insert[] = {};
//	printf("请输入仓库编号> ");
	scanf("%d",&(goods->warehouse_no));

//	printf("请输入货物编号> ");
	scanf("%d",&(goods->.goods_no));

//	printf("请输入入库时间> ");
	scanf("%d",&(goods->.info_time));

//	printf("请输入是否出货> ");
	scanf("%s",&(goods->empty_status));

	sprintf(buf,"insert into goods_info value(%d,%d,%d,%s)",goods->warehouse_no,goods->goods_no,goods->info_time[10],goods->empty_status);

	
}
//阈值的更新
void humidity_add(int socket_fd,threshold_t *th)
{
	char buf[200] = {0};
	int a,b;
//	char insert[] = {};
//	printf("请输入温度上限> ");
	scanf("%d",&(th->temperature_max));

//	printf("请输入温度下限> ");
	scanf("%d",&(th->temperature_min));
//	a = select *from threshold_value
}


//	printf("请输入湿度上限> ");
	scanf("%d",&(th->humidity_max));
	
//	printf("请输入湿度下限> ");
	scanf("%d",&(th->humidity_min));

//	printf("请输入最大光照强度> ");
	scanf("%d",&(th->light_max));

//	printf("请输入最小光照强度> ");
	scanf("%d",&(th->light_min));
	
	sprintf(buf,"insert into threshold_value value(%d,%d,%d,%d,%d,%d)",th->temperature_max,th->temperature_min,th->humidity_max,th->humidity_min,th->light_max,th->light_min);



}


void goods_change(int socket_fd,goods_msg *goods)
{
	int a = 0;
	char buf[200] = {};
	scanf("%d",&a);

	switch(a)
	{
		case 1:
			printf("输入要修改的货物标号> ");
			scanf("%d",&(goods->goods_no));
			
			printf("要修改成的> ");
			scanf("%d",&(goods->goods_no));
			sprintf(buf,"update goods_info set goods_no=%d where goods_no=%d",goods->goods_no,goods->goods_no);
			break;

		case 2:
			printf("修改货物入库时间 \n");
			printf("输入要修改时间的货物标号> ");
			scanf("%d",&(goods->goods_no));
			
			printf("入库时间修改成的> ");
			scanf("%d",&(goods->info_time));
			sprintf(buf,"update goods_info set info_time=%d where goods_no=%d",goods->info_time,goods->goods_no);
			break;
		case 3:
			printf("输入要修改出货的货物标号> ");
			scanf("%d",&(goods->goods_no));
			
			printf("出货情况修改成的> ");
			scanf("%d",&(goods->empty_status));
			sprintf(buf,"update goods_info set empty_status=%d where goods_no=%d",goods->empty_status,goods->goods_no);
			break;
		case 4:
			printf("退出！");


			

	}

}

#if 0
void humidity_change(int socket_fd,threshold_t *th)
{
	int b = 0;
	char buf[200] = [];
	printf("1.温度上限 2.温度下限 3.湿度上限 ");
	printf("4.湿度下限 5.最大光照 6.最小光照 ");
	scanf("%d",&b);

	switch(b)
	{
		case 1:


	}


}
#endif

void admin_query(int socket_fd,goods_msg *goods)
{
	
}







